/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/1/23
 */
import React, {Component} from 'react';
import { Form, Input,Modal} from "antd"
import {checkInputCode, getUserInfo} from "../../../commons/utils/CommonUtils";

const FormItem = Form.Item;
const formLayout = {
    labelCol: {span: 6},
    wrapperCol: {span: 16}
}

class EditRoleGroupModal extends Component {
    getFormValue = () => {
        let result = false;
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                result = values;
                result.code = getUserInfo().tenantCode+"-"+result.code;
                const {isEdit,editData} = this.props;
                if (isEdit && editData) {
                    result = {...editData,...result}
                }
            }
        });
        return result;
    }
    componentDidMount(){
        this.props.onRef && this.props.onRef(this);
    }
    render() {
        const {title, visible, onOk, onCancel,isEdit, editData,confirmLoading} = this.props;
        const {getFieldDecorator} = this.props.form;
        const {tenantCode=""} = getUserInfo();
        const maxGroupCodeLen = 20 - tenantCode.length - 1;
        return (
            <Modal
                width={500}
                centered
                destroyOnClose={true}
                maskClosable={false}
                title={title}
                confirmLoading={confirmLoading}
                visible={visible}
                onCancel={onCancel}
                onOk={onOk}
            >
                <Form>
                    <FormItem
                        label={"代码"}
                        {...formLayout}
                    >
                        {getFieldDecorator('code', {
                            initialValue: isEdit && editData && editData.code ? editData.code.split("-")[1] : "",
                            rules: [
                                   {required: true, message: '请输入代码'},
                                   {max: maxGroupCodeLen,message: `输入的最大长度为${maxGroupCodeLen}`},checkInputCode()
                                ]
                        })(
                            <Input addonBefore={`${tenantCode}-`}/>
                        )}
                    </FormItem>
                    <FormItem
                        label={"名称"}
                        {...formLayout}
                    >
                        {getFieldDecorator('name', {
                            initialValue: isEdit && editData && editData.name ? editData.name : "",
                            rules: [{required: true, message: '请填写名称!'},{max:50,message:'长度不超过50个字符！'}]
                        })(
                            <Input placeholder={"请填写名称"}/>
                        )}
                    </FormItem>
                </Form>
            </Modal>
        );
    }
}

export default Form.create()(EditRoleGroupModal);
